package com.xuecheng.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.basic.domain.Category;
import com.xuecheng.basic.mapper.CategoryMapper;
import com.xuecheng.basic.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.model.vo.CategoryVo;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-30
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    // 查询所有课程分类
    @Override
    public ResponseResult findAll() {
        // 1.查询一级节点分类（parentId=0）
        LambdaQueryWrapper<Category> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(Category::getParentid, "0");
        Category root = this.getOne(qw1);
        // 一级节点转vo
        CategoryVo rootVo = BeanHelper.copyProperties(root, CategoryVo.class);
        // 2.查询二级节点分类（parendId=一级id）
        LambdaQueryWrapper<Category> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(Category::getParentid,rootVo.getId() );
        List<Category> secondList = this.list(qw2);
        // 二级节点转vo
        List<CategoryVo> secondVoList = BeanHelper.copyWithCollection(secondList, CategoryVo.class);
        // 一级节点添加二级节点
        rootVo.setCategoryTreeNodes(secondVoList);
        // 3.查询三级节点分类（parendId=二级id）
        for (CategoryVo secondVo : secondVoList) {
            LambdaQueryWrapper<Category> qw3 = new LambdaQueryWrapper<>();
            qw3.eq(Category::getParentid, secondVo.getId());
            List<Category> threeList = this.list(qw3);
            // 三级接点转vo
            List<CategoryVo> threeVoList = BeanHelper.copyWithCollection(threeList, CategoryVo.class);
            // 二级节点添加三级节点
            secondVo.setCategoryTreeNodes(threeVoList);
        }
        // 4.返回一级节点vo
        return ResponseResult.okResult(rootVo);
    }
}
